home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / dsniff / decode_postgresql.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-17  |  1.0 KB  |  59 lines

  1. /*
  2.   decode_postgresql.c
  3.  
  4.   PostgreSQL.
  5.  
  6.   Thanks to Eric Jackson <shinobi@monkey.org> for packet traces.
  7.   
  8.   Copyright (c) 2000 Dug Song <dugsong@monkey.org>
  9.  
  10.   $Id: decode_postgresql.c,v 1.1 2000/05/17 00:34:49 dugsong Exp $
  11. */
  12.  
  13. #include <sys/types.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "decode.h"
  17.  
  18. #define STARTUP_PKTLEN    296
  19.  
  20. int
  21. decode_postgresql(u_char *buf, int len)
  22. {
  23.     u_long plen;
  24.     u_char *p;
  25.     char *db, *user;
  26.     
  27.     if (len < STARTUP_PKTLEN)
  28.         return (0);
  29.     
  30.     Buf[0] = '\0';
  31.     db = user = NULL;
  32.     
  33.     for (;;) {
  34.         if (len < 4) break;
  35.         plen = ntohl(*(u_long *) buf);
  36.         
  37.         if (plen > len)    break;
  38.         p = buf + 4;
  39.         
  40.         if (plen == STARTUP_PKTLEN) {
  41.             if (ntohl(*(u_long *)p) >> 16 == 2) {
  42.                 db = p + 4; db[63] = '\0';
  43.                 user = db + 64; user[31] = '\0';
  44.             }
  45.         }
  46.         else if (db != NULL && user != NULL) {
  47.             buf[plen - 1] = '\0';
  48.             snprintf(Buf + strlen(Buf),
  49.                  sizeof(Buf) - strlen(Buf),
  50.                  "%s\n%s\n%s\n", db, user, p);
  51.             db = user = NULL;
  52.         }
  53.         buf += plen;
  54.         len -= plen;
  55.     }
  56.     return (strlen(Buf));
  57. }
  58.  
  59.